package com.cily.utils.t_webrtc;

import android.app.Application;

import com.cily.utils.app.utils.SpUtils;
import com.cily.utils.app.utils.TimerUtils;
import com.cily.utils.base.StrUtils;
import com.cily.utils.base.time.TimeUtils;
import com.cily.utils.log.L;
import com.cily.utils.t_webrtc.utils.StrMsgUtils;
import com.cily.utils.t_webrtc.utils.WsUtils;

/**
 * user:cily
 * time:2017/5/10
 * desc:
 */

public class MyApp extends Application {
    private static MyApp app;
    private final String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
    }

    private TimerUtils timer;
    public void timer(){
        if (timer == null){
            timer = new TimerUtils(Conf.ALIVE_TIME) {
                @Override
                public void onTick(long time) {
                    if (WsUtils.closeByUser){
                        L.i(TAG, "用户退出登录了:" + TimeUtils.milToStr(System.currentTimeMillis(), null));
                    }else{
                        L.i(TAG, "定时器时间到了:" + TimeUtils.milToStr(System.currentTimeMillis(), null));

                        String userRoom = SpUtils.getStr(app, Conf.USER_ROOM, null);
                        if (StrUtils.isEmpty(userRoom)){

                        }else{
                            if (WsUtils.isConnectedAndRestart(app, userRoom)){
                                WsUtils.sendMsg(StrMsgUtils.alive(userRoom));
                            }
                        }
                    }
                }
            };
        }
        timer.cancel();
        timer.start();
    }

    public static MyApp getApp(){
        return app;
    }
}
